package com.lyl.lawyer.service.impl;

import com.cn.lyl.lawyer.dao.ServiceMapper;
import com.cn.lyl.lawyer.dao.ServiceTypeDependenceMapper;
import com.cn.lyl.lawyer.dao.ServiceTypeMapper;
import com.cn.lyl.lawyer.model.entity.ServiceModel;
import com.cn.lyl.lawyer.model.entity.ServiceTypeDependenceModel;
import com.cn.lyl.lawyer.model.entity.ServiceTypeModel;
import com.cn.lyl.lawyer.model.vo.ServiceTypeVo;
import com.cn.lyl.lawyer.service.ServiceService;
import com.cn.lyl.lawyer.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ServiceServiceImpl implements ServiceService {
    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private ServiceTypeMapper serviceTypeMapper;
    @Autowired
    private ServiceTypeDependenceMapper serviceTypeDependenceMapper;

    @Override
    public List<ServiceTypeVo> findAll(ServiceTypeVo serviceTypeVo) {
        return serviceMapper.findAll(serviceTypeVo);
    }

    @Override
    public List<ServiceTypeVo> findByType(ServiceTypeVo serviceTypeVo) {
        return serviceMapper.findByType(serviceTypeVo);
    }

    @Override
    public int getCount(ServiceTypeVo serviceTypeVo) {
        return serviceMapper.countFindAll(serviceTypeVo);
    }

    @Override
    @Transactional
    public void add(ServiceTypeVo serviceTypeVo) {
        ServiceModel serviceModel = new ServiceModel();
        serviceModel.setFace(Tool.stringIsNotNull(serviceTypeVo.getFace()));
        serviceModel.setFlow(Tool.stringIsNotNull(serviceTypeVo.getFlow()));
        serviceModel.setIntro(Tool.stringIsNotNull(serviceTypeVo.getIntro()));
        serviceModel.setServiceName(Tool.stringIsNotNull(serviceTypeVo.getServiceName()));
        serviceModel.setPrice(Tool.floatIfNotNull(serviceTypeVo.getPrice()));
        serviceModel.setMarketPrice(Tool.floatIfNotNull(serviceTypeVo.getMarketPrice()));
        serviceMapper.add(serviceModel);
        ServiceTypeModel byType = serviceTypeMapper.findByType(serviceTypeVo.getType());
        ServiceTypeDependenceModel serviceTypeDependenceModel = new ServiceTypeDependenceModel();
        serviceTypeDependenceModel.setServiceId(serviceModel.getId());
        serviceTypeDependenceModel.setTypeId(byType.getId());
        serviceTypeDependenceMapper.insert(serviceTypeDependenceModel);

    }

    @Override
    @Transactional
    public void delete(Integer id) {
       serviceMapper.delete(id);
       serviceTypeDependenceMapper.deleteByServiceId(id);
    }

    @Override
    public ServiceTypeVo findById(Integer id) {
        return serviceMapper.findById(id);
    }

    @Override
    @Transactional
    public void change(ServiceTypeVo serviceTypeVo) {
        ServiceModel serviceModel = new ServiceModel();
        serviceModel.setFace(Tool.stringIsNotNull(serviceTypeVo.getFace()));
        serviceModel.setFlow(Tool.stringIsNotNull(serviceTypeVo.getFlow()));
        serviceModel.setIntro(Tool.stringIsNotNull(serviceTypeVo.getIntro()));
        serviceModel.setServiceName(Tool.stringIsNotNull(serviceTypeVo.getServiceName()));
        serviceModel.setPrice(Tool.floatIfNotNull(serviceTypeVo.getPrice()));
        serviceModel.setMarketPrice(Tool.floatIfNotNull(serviceTypeVo.getMarketPrice()));
        serviceModel.setId(serviceTypeVo.getId());
        serviceMapper.update(serviceModel);
        ServiceTypeModel byType = serviceTypeMapper.findByType(serviceTypeVo.getType());
        ServiceTypeDependenceModel serviceTypeDependenceModel = new ServiceTypeDependenceModel();
        serviceTypeDependenceModel.setServiceId(serviceModel.getId());
        serviceTypeDependenceModel.setTypeId(byType.getId());
        serviceTypeDependenceMapper.insert(serviceTypeDependenceModel);

    }



    @Override
    public List<ServiceTypeModel> findAllType() {
        return serviceTypeMapper.findAll();
    }
}
